package com.stylefeng.guns.modular.model.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.model.service.IOrderService;
import com.stylefeng.guns.modular.system.model.Order;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.TicketNum;
import com.stylefeng.guns.modular.model.service.ITicketNumService;

import java.io.IOException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * model控制器
 *
 * @author fengshuonan
 * @Date 2018-08-06 10:19:04
 */
@Controller
@RequestMapping("/ticketNum")
public class TicketNumController extends BaseController {

    private String PREFIX = "/model/ticketNum/";

    @Autowired
    private ITicketNumService ticketNumService;

    @Autowired
    private IOrderService orderService;


    @RequestMapping("/tiketone")
    @ResponseBody
    public String tiketone(@RequestParam("lineid")Integer lineid) {
        TicketNum ticketNum1=null;
        try {
        TicketNum ticketNum = new TicketNum();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String gotontime = sdf.format(new Date());
        ticketNum1= ticketNumService.getTicketnum(lineid, gotontime);
        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

       if (ticketNum1==null){}
//        if (lock.tryLock()) {
//            try {
//
//                if (ticketNumService.getTicketnum(order.getLineid(), gotontime) == null) {
//                    String digitalCode = orderController.arrlist();
//                    ticketNum.setRemainingnum(44);  //需要修改，如果座位不是45系统就崩了
//                    ticketNum.setLineid(order.getLineid());
//                    ticketNum.setTicketdate(gotontime);
//                    ticketNum.setOrderid(order.getId());
//                    ticketNum.setDigitalcode(digitalCode);
//
//                    ticketNumService.insert(ticketNum);
//                } else {
//                    //扣减优惠券数量(to-do 后期需要修改，考虑并发问题)
////                    TicketPrice tock = ticketPriceService.selectById(order.getTicketpriceid());
//                    TicketNum ticketNum1 = ticketNumService.getTicketnum(order.getLineid(), gotontime);
//                    if (ticketNum1.getRemainingnum() < 1 || ticketNum1.getRemainingnum() < payNum) {
//                        throw new RuntimeException("余票不足!");
//                    }
//                    ticketNum1.setRemainingnum(ticketNum1.getRemainingnum() - 1);
//                    ticketNumService.updateById(ticketNum1);
//
//                }
//
//
//            } catch (Exception e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            } finally {
//                lock.unlock();
//            }
//        }
        return null;


    }

    @RequestMapping("/digitalcode")
    @ResponseBody
    public Object digitalcode(@RequestParam("orderid")Integer orderid) {
        Order order=orderService.selectById(orderid);  //通过订单id查询出路线
        Integer lineids=order.getLineid();
        Date tickettime=order.getTickettime();//ticket为有效时间
        Date date=new Date();//生成数字码的实际应该是同一天   //数字嘛的有效时间是一天
        Integer state=order.getState();
        if (tickettime.getTime()>=date.getTime()&&state==1){   //当前时间下于车票有效时间的时候显示数字码
        Date geton=    order.getGetontime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String gotontime = sdf.format(geton);
        return  ticketNumService.getTicketnum(lineids,gotontime);
        }
        return  null;

    }

    /*
      微信小程序删除之后重新登录 需要获取行车码
     */
    @RequestMapping("/wxTicketNum")
    @ResponseBody
    public Object wxTicketNum(@RequestParam("orderId")Integer orderId) throws IOException {
        /*
        通过userid查询出用户的订单，然后通过订单的发车时间和订单状态判断出是否显示数字码，然后通过订单的线路id和发车时间查询出数字码
         */
        Order order=  orderService.selcetByOrderdState(orderId,1);
        Integer lineid=order.getLineid();
        Date tickettime=order.getTickettime();//ticket为有效时间
        Date date=new Date();//生成数字码的实际应该是同一天   //数字嘛的有效时间是一天
        Integer state=order.getState();
        if (tickettime.getTime()>=date.getTime()){   //当前时间下于车票有效时间的时候显示数字码
            Date geton=    order.getGetontime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String gotontime = sdf.format(geton);
            return  ticketNumService.getTicketnum(lineid,gotontime);
        }

        return null;
    }

    /**
     * 跳转到model首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "ticketNum.html";
    }

    /**
     * 跳转到添加model
     */
    @RequestMapping("/ticketNum_add")
    public String ticketNumAdd() {
        return PREFIX + "ticketNum_add.html";
    }

    /**
     * 跳转到修改model
     */
    @RequestMapping("/ticketNum_update/{ticketNumId}")
    public String ticketNumUpdate(@PathVariable Integer ticketNumId, Model model) {
        TicketNum ticketNum = ticketNumService.selectById(ticketNumId);
        model.addAttribute("item",ticketNum);
        LogObjectHolder.me().set(ticketNum);
        return PREFIX + "ticketNum_edit.html";
    }

    /**
     * 获取model列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return ticketNumService.selectList(null);
    }

    /**
     * 新增model
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TicketNum ticketNum) {
        ticketNumService.insert(ticketNum);
        return SUCCESS_TIP;
    }

    /**
     * 删除model
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer ticketNumId) {
        ticketNumService.deleteById(ticketNumId);
        return SUCCESS_TIP;
    }

    /**
     * 修改model
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TicketNum ticketNum) {
        ticketNumService.updateById(ticketNum);
        return SUCCESS_TIP;
    }

    /**
     * model详情
     */
    @RequestMapping(value = "/detail/{ticketNumId}")
    @ResponseBody
    public Object detail(@PathVariable("ticketNumId") Integer ticketNumId) {
        return ticketNumService.selectById(ticketNumId);
    }
}
